package com.powernode.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import org.springframework.security.oauth2.provider.token.store.KeyStoreKeyFactory;


import java.security.KeyPair;


@Configuration
@EnableAuthorizationServer
public class AuthTokenConfig extends AuthorizationServerConfigurerAdapter {
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private AuthenticationManager authenticationManager;

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        super.configure(security);
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                .withClient("web")
                .secret(new BCryptPasswordEncoder().encode("web-secret"))
                .scopes("password")
                .accessTokenValiditySeconds(7200)
                .redirectUris("https://www.baidu.com")
                .authorizedGrantTypes("password")
                .and()
                .withClient("client")
                .secret(new BCryptPasswordEncoder().encode("client-secret"))
                .scopes("password")
                .accessTokenValiditySeconds(Integer.MAX_VALUE)
                .redirectUris("http://www.baidu.com")
                .authorizedGrantTypes("client_credentials");

    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
//        super.configure(endpoints);
        endpoints.tokenStore(tokenStore())
                .accessTokenConverter(jwtTokenConverter())
                .authenticationManager(authenticationManager);
    }

    @Bean
    public JwtAccessTokenConverter jwtTokenConverter() {
        JwtAccessTokenConverter tokenConverter = new JwtAccessTokenConverter();
        ClassPathResource pathResource = new ClassPathResource("cxs-jwt.jks");
        KeyStoreKeyFactory keyFactory = new KeyStoreKeyFactory(pathResource, "cxs123".toCharArray());
        KeyPair key = keyFactory.getKeyPair("cxs-jwt");
        tokenConverter.setKeyPair(key);
        return tokenConverter;
    }

    @Bean
    public TokenStore tokenStore() {
        JwtTokenStore jwtTokenStore = new JwtTokenStore(jwtTokenConverter());
        return jwtTokenStore;
    }

}
